package com.zyz.K_集合.Map接口三TreeMap;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;

/**
 * @author: vick
 * @Date: 2025/3/30
 * @Description:
 * @version: 1.0
 */
public class User2Test {
    public static void main(String[] args) {

        Comparator comparator = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof User2 && o2 instanceof User2) {
                    User2 user1 = (User2) o1;
                    User2 user2 = (User2) o2;
                    return user1.getName().compareTo(user2.getName());
                }
                throw new RuntimeException("类型不匹配");
            }
        };
        TreeMap treeMap = new TreeMap(comparator);
        treeMap.put(new User2("vick", 18), 18);
        treeMap.put(new User2("Tom", 28), 28);
        treeMap.put(new User2("lili", 38), 38);
        treeMap.put(new User2("nana", 18), 18);

        Set entrySet = treeMap.entrySet();
        for (Object obj : entrySet) {
            System.out.println(obj);
        }
    }
}
